﻿using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Users;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Tokens;

using System;
using System.Globalization;
using System.IO;


namespace DotNetNuke.Modules.UserDefinedTable
{


  public class DownloadPropertyAccess : IPropertyAccess
  {
    private DotNetNuke.Services.FileSystem.FileInfo currentFile;
    private string FileIdentifier;
    private int ModuleId;
    private int PortalId;

    public DownloadPropertyAccess(string id, int PortalId, int ModuleID)
    {
      this.FileIdentifier = id;
      this.PortalId = PortalId;
      this.ModuleId = ModuleID;
    }

    public string GetProperty(string strPropertyName, string strFormat, CultureInfo formatProvider, UserInfo AccessingUser, Scope AccessLevel, ref bool PropertyNotFound)
    {
      string format = string.Empty;
      if ( string.IsNullOrEmpty(strFormat) )
      {
        format = "g";
      }
      else
      {
        format = strFormat;
      }
      strPropertyName = strPropertyName.ToLowerInvariant();
      if ((this.File != null) || (strPropertyName == "clicks"))
      {
        switch (strPropertyName)
        {
          case "name":
            return PropertyAccess.FormatString(this.File.FileName, strFormat);

          case "folder":
            return PropertyAccess.FormatString(new FolderController().GetFolderInfo(this.PortalId, this.File.FolderId).FolderName, strFormat);

          case "path":
            return PropertyAccess.FormatString(new FolderController().GetFolderInfo(this.PortalId, this.File.FolderId).FolderPath, strFormat);

          case "size":
            return this.File.Size.ToString(format, formatProvider);

          case "sizemb":
            {
              double num2 = ((double)this.File.Size) / 1048576.0;
              return num2.ToString(format, formatProvider);
            }
          case "clicks":
            {
              UrlTrackingInfo info = new UrlController().GetUrlTracking(this.PortalId, this.FileIdentifier, this.ModuleId);
              if (info != null)
              {
                return info.Clicks.ToString(format, formatProvider);
              }
              return "";
            }
          case "extension":
            return PropertyAccess.FormatString(this.File.Extension, strFormat);
        }
      }
      PropertyNotFound = true;
      return string.Empty;
    }

    public CacheLevel Cacheability
    {
      get
      {
        return CacheLevel.fullyCacheable;
      }
    }

    //public CacheLevel DotNetNuke.Services.Tokens.IPropertyAccess.Cacheability
    //{
    //    get
    //    {
    //        return CacheLevel.fullyCacheable;
    //    }
    //}

    private DotNetNuke.Services.FileSystem.FileInfo File
    {
      get
      {
        if (this.currentFile == null)
        {
          FileController controller = new FileController();
          //if (LikeOperator.LikeString(this.FileIdentifier, "FileID=*", CompareMethod.Binary))
          if (this.FileIdentifier.StartsWith("FileID=", StringComparison.OrdinalIgnoreCase))
            this.currentFile = controller.GetFileById(int.Parse(UrlUtils.GetParameterValue(this.FileIdentifier)), DotNetNuke.Common.Globals.GetPortalSettings().PortalId);

          else if (DotNetNuke.Common.Globals.GetURLType(this.FileIdentifier) == TabType.File)
          {
            FolderController controller2 = new FolderController();
            string fileName = Path.GetFileName(this.FileIdentifier);
            FolderInfo info2 = controller2.GetFolder(this.PortalId, this.FileIdentifier.Replace(fileName, ""), false);
            if (info2 != null)
              this.currentFile = controller.GetFile(fileName, this.PortalId, info2.FolderID);

          }
        }
        return this.currentFile;
      }
    }



  }

}

